package com.jic.market.controller.manage;

import com.jic.common.base.vo.RestResult;
import com.jic.market.PromotionUpdatePresellInfoRequest;
import com.jic.market.api.manage.PromotionMerchantProductApi;
import com.jic.market.entity.PromotionMerchantProductEntity;
import com.jic.market.request.*;
import com.jic.market.response.PageResponse;
import com.jic.market.response.PromotionProductResponse;
import com.jic.market.service.PromotionLimitRuleService;
import com.jic.market.service.PromotionMerchantProductService;
import com.jic.market.service.PromotionSingleLimitRuleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


/**
 * description: PromotionMerchantProductController
 * date: 2020/1/17 17:07
 * author: admin
 * version: 1.0
 */
@RestController
@RequestMapping("promotionMerchantProduct")
public class PromotionMerchantProductController implements PromotionMerchantProductApi {
    @Autowired
    PromotionMerchantProductService promotionMerchantProductService;
    @Autowired
    PromotionLimitRuleService promotionLimitRuleService;
    @Autowired
    PromotionSingleLimitRuleService promotionSingleLimitRuleService;

    @PostMapping("deleteBatch")
    @Override
    public RestResult deleteBatch(@RequestBody PromotionMerchantProductRequest request) {
        promotionMerchantProductService.deleteBatch(request.getIds());
        return RestResult.success(null);
    }

    @PostMapping("updatePriceOrLimit")
    @Override
    public RestResult updatePriceOrLimit(@RequestBody PromotionLimitRuleRequest request) {
        promotionMerchantProductService.updateEntityByPromotionIdAndMpId(request.getPromPrice(),request.getPromotionId(),request.getMpId());
        promotionLimitRuleService.updatePriceOrLimit(request);
        return RestResult.success(null);
    }

    @PostMapping("deleteProduct")
    @Override
    public RestResult deletePromotionMerchantProduct(
            @RequestBody DeletePromotionMerchantProductRequest request) {
        //根据mpId ，promotionId 查询
        PromotionMerchantProductEntity pmpEntity =
                promotionMerchantProductService.queryEntityByPromotionIdAndMpId(
                        request.getPromotionId(), request.getMpId());
//        if (pmpEntity != null){
//            pmpEntity.getMpParentId();
//        }
        // TODO 步骤1.    如果是普通商品可以直接删除
        // TODO 步骤2.    如果是子品，需要先查询有没有其他同样父品的子品
        // TODO 步骤3.    如果还有其他子品，则只删除此子品
        // TODO 步骤4.    如果没有其他子品，则主品和此子品

        Integer count1 =promotionMerchantProductService.deleteEntityByPromotionIdAndMpId(
                request.getPromotionId(),request.getMpId());
        Integer count2 =promotionLimitRuleService.deleteEntityByPromotionIdAndMpId(
                request.getPromotionId(),request.getMpId());

        // promotion_mutex  deleteFlag 设置为1

        return RestResult.success(count1);
    }

   /*
   * author:zxl
   * data:2020/3/12
   * description：单一促销商品优惠及限购修改
   * */
    @PostMapping("updateSinglePriceOrLimit")
    public RestResult updateSinglePriceOrLimit(@RequestBody PromotionLimitRuleRequest request) {
        // 修改 促销商品表(商家) promotion_merchant_product
        promotionMerchantProductService.updateEntityByPromotionIdAndMpId(request.getPromPrice(),request.getPromotionId(),request.getMpId());
        // 修改 促销限制规则表 promotion_limit_rule
        promotionLimitRuleService.updatePriceOrLimit(request);
        // 修改 单一促销代理商规则表 promotion_single_rule
        promotionSingleLimitRuleService.updatePriceOrLimit(request);
        return RestResult.success(null);
    }

    /**
     * 活动批量添加商品
     * @param request
     * @return
     */
    @PostMapping("saveBatch")
    @ApiOperation(value = "活动批量添加商品")
    public RestResult saveBatch(@RequestBody PromotionProductSaveBatchListRequest request) {
        return RestResult.success(promotionMerchantProductService.saveBatchV3(request));
    }


    /**
     * 分页查询
     * @param request
     * @return
     */
    @PostMapping("/getProductList")
    @ApiOperation(value = "分页查询")
    public RestResult<PageResponse<PromotionProductResponse>> getProductList(@RequestBody PromotionMerchantProductRequest request){
        return RestResult.success(promotionMerchantProductService.getProductList(request));
    }

    @PostMapping("deleteProductV2")
    public RestResult deleteProductV2(
            @RequestBody DeletePromotionMerchantProductRequest request) {
        return RestResult.success(promotionMerchantProductService.deleteProductV2(request.getId()));
    }

    @PostMapping("/addAllProduct")
    public RestResult<Integer> addAllProduct(@RequestBody PromotinAddAllProductRequest request) {
        return RestResult.success(promotionMerchantProductService.addAllProduct(request));
    }

    @PostMapping("/skuCount")
    public RestResult<Long> skuCount(@RequestBody PromotionProductSkuCountRequest request) {
        return RestResult.success(promotionMerchantProductService.skuCount(request));
    }

    @PostMapping("updatePresellInfo")
    public RestResult updatePresellInfo(@RequestBody PromotionUpdatePresellInfoRequest request) {
        promotionMerchantProductService.updatePresellInfo(request);
        return RestResult.success(null);
    }


    @PostMapping("/renewAddMutexProduct")
    public RestResult<Map<String,Object>> renewAddMutexProduct(@RequestBody RenewAddMutexProductRequest renewAddMutexProductRequest){
        return promotionMerchantProductService.renewAddMutexProduct(renewAddMutexProductRequest);
    }


}
